package com.example.opengldemo.base;

import android.opengl.GLES30;
import android.util.Log;
import android.view.View;

import com.example.opengldemo.util.ShaderBuffer;
import com.example.opengldemo.util.ShaderHandle;
import com.example.opengldemo.util.ShaderUtil;

public class Obj_Vertex_Normal_Texcoors_Demo {

    String vertexShaderPath = "base/vertex_object_vnt.sh";
    String fragShaderPath = "base/frag_object_vnt.sh";

    public ShaderHandle shaderHandle;
    public ShaderBuffer shaderBuffer;

    private float[] vertexArray = null ;
    private float[] normalArray = null ;
    private float[] texcoorsArray = null ;
    int texture2 = 0 ;
    int texture3 = 0 ;
    int uBoolean = 0 ;

    public Obj_Vertex_Normal_Texcoors_Demo(View view , float[] vertexArray , float[] normalArray , float[] texcoorsArray){
        this.vertexArray = vertexArray ;
        this.normalArray = normalArray ;
        this.texcoorsArray = texcoorsArray ;
        initProgram(view);
        initBuffer();
    }

    public Obj_Vertex_Normal_Texcoors_Demo(View view , float[] vertexArray , float[] normalArray , float[] texcoorsArray , String vertex , String fragment){
        this(view , vertexArray , normalArray , texcoorsArray) ;
    }

    private void initProgram(View view) {
        String vertexShaderRes = ShaderUtil.loadFromAssetsFile(vertexShaderPath, view.getResources());
        String fragShaderRes = ShaderUtil.loadFromAssetsFile(fragShaderPath, view.getResources());
        initHandle(ShaderUtil.createProgram(vertexShaderRes, fragShaderRes));
    }
    private void initHandle(int mProgram) {
        shaderHandle = new ShaderHandle(mProgram);
    }
    private void initBuffer() {
        shaderBuffer = new ShaderBuffer();
        shaderBuffer.setVertexBuffer(getVertexArray());
        shaderBuffer.setNormalBuffer(getNormalArray());
        shaderBuffer.setTexcoorBuffer(getTexcoorsArray());
    }

    private float[] getVertexArray() {
        shaderBuffer.vCount = vertexArray.length/3 ;
        return vertexArray;
    }
    private float[] getNormalArray() {
        return normalArray;
    }
    private float[] getTexcoorsArray() {
        return texcoorsArray;
    }

    public void drawSelf(int textureid ) {
        GLES30.glUseProgram(shaderHandle.getProgram());
        GLES30.glVertexAttribPointer(
                shaderHandle.get_A_PositionHandle(),
                3,
                GLES30.GL_FLOAT,
                false,
                3 * 4,
                shaderBuffer.getVertexBuffer()
        );
        if(shaderBuffer.getNormalBuffer() != null){
            GLES30.glVertexAttribPointer(
                    shaderHandle.get_A_NormalHandle() ,
                    3,
                    GLES30.GL_FLOAT,
                    false,
                    3 * 4,
                    shaderBuffer.getNormalBuffer()
            );
        }
        if(shaderBuffer.getTexcoorBuffer() != null){
            GLES30.glVertexAttribPointer(
                    shaderHandle.get_A_TexCoorsHandle() ,
                    2,
                    GLES30.GL_FLOAT,
                    false,
                    2 * 4,
                    shaderBuffer.getTexcoorBuffer()
            );
        }

        GLES30.glUniformMatrix4fv(
                shaderHandle.get_U_MVPMatrixHandle(),
                1,
                false,
                shaderBuffer.getMVPMatrixArray(),
                0
        );
        GLES30.glUniformMatrix4fv(
                shaderHandle.get_U_MMatrixhandle(),
                1,
                false,
                shaderBuffer.getMMatrixArray(),
                0
        );
        GLES30.glUniform3fv(
                shaderHandle.get_U_LightLHandle(),
                1,
                shaderBuffer.getLightLocationBuffer()
        );
        GLES30.glUniform3fv(
                shaderHandle.get_U_CameraHandle(),
                1,
                shaderBuffer.getCameraBuffer()
        );
        GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D , textureid);

        GLES30.glEnableVertexAttribArray(shaderHandle.get_A_PositionHandle());
        GLES30.glEnableVertexAttribArray(shaderHandle.get_A_NormalHandle());
        GLES30.glEnableVertexAttribArray(shaderHandle.get_A_TexCoorsHandle());

        GLES30.glDrawArrays(GLES30.GL_TRIANGLES, 0 ,  shaderBuffer.vCount);
    }
}
